#include "head.h"
//张亚伦的表------------------------------------------
//建登录用的user表
void create_ALL(){
    create_db();//用户表
    create_resident();//住户表
    create_floor();//楼栋表
    create_house();//房屋表
    create_payment();//缴费表
    create_rep();//报修表
    create_table_comp();//投诉表
    create_table_park();//车位表
}
//登录表
void create_db(){
    char * a="create table if not exists user(id integer primary key,"
             "account text not null unique,"
             "password text not null);";
    int ls = sqlite3_exec(db,a,NULL,NULL,NULL);
    if (ls!=SQLITE_OK){
        fprintf(stderr,"建表失败！%s\n", sqlite3_errmsg(db));
    }
}
//创建楼栋管理表
void create_floor(){
    int ls = sqlite3_exec(db,"create table if not exists floor("
                             "floor_id integer primary key,"
                             "floor_name text unique NOT NULL,"
                             "tier_count integer,"
                             "time text);",NULL,NULL,NULL);
    if(ls!=SQLITE_OK){
        fprintf(stderr,"建表失败:%s\n", sqlite3_errmsg(db));
    }
}
//住户表
void create_resident(){
    int ls = sqlite3_exec(db,"create table if not exists resident(person_id integer primary key,"
                             "name text not null,"
                             "phone text,"
                             "id_card text unique,"
                             "time text);",NULL,NULL,NULL);
    if(ls!=SQLITE_OK){
        sprintf(stderr,"创建失败:%s\n", sqlite3_errmsg(db));
    }
}
//创建房屋表
void create_house(){
    int ls= sqlite3_exec(db,"create table if not exists house("
                            "house_id integer primary key,"
                            "floor_name text not null,"
                            "person_id integer not null,"
                            "house_num text unique not null,"
                            "house_type text,"
                            "time text"
                            ");",NULL,NULL,NULL);
    if(ls!=SQLITE_OK){
        fprintf(stderr,"建表失败:%s", sqlite3_errmsg(db));
    }
}
//张亚伦的表--------------------------
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//郭佳佳的表--------------------------
//缴费表
int create_payment(){
    //定义建表语句
    const char * create_payment =
            "create table if not exists payment ("
            " pay_id integer primary key,"
            " person_id integer  not null,"
            " price real,"
            " ynpay text"
            ");";
    char * errmsg;
    int rc = sqlite3_exec(db,create_payment,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"create_payment 建表失败:%s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }

    return 0;
}
//建报修表
int create_rep(){
    //定义建表语句
    const char * create_Repair =
            "create table if not exists Repair ("
            " repair_id integer primary key,"
            " person_id integer  not null,"
            " reason_repair text,"
            " ynrepair text"
            ");";
    char * errmsg;
    int rc = sqlite3_exec(db,create_Repair,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"create_Repair 建表失败:%s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }

    return 0;
}
//郭佳佳的表--------------------------
//|||||||||||||||||||||||||||||||||||||||||||||||||||
//曾英杰的表--------------------------
//创建投诉表
int create_table_comp(){
    const char * create_comp=
            " create table if not exists comp("
            " person_id integer primary key,"
            " comp_con text,"
            " reply text"
            " );";
    char * errmsg;
    int rc = sqlite3_exec(db,create_comp,NULL,NULL,&errmsg) ;
    if(rc!=SQLITE_OK ){
        fprintf(stderr,"table comp建表失败：%s\n",errmsg);
        sqlite3_free(errmsg);
        return-1;
    }
 //   printf("table_comp建表成功！\n");
    return 0;
}
//创建车位表
int create_table_park(){
    const char * create_park=
            " create table if not exists park("
            " person_id integer ,"
            " park_number text primary key,"
            " park_type text,"
            " park_location text"
            " );";
    char * errmsg;
    int rc = sqlite3_exec(db,create_park,NULL,NULL,&errmsg) ;
    if(rc!=SQLITE_OK ){
        fprintf(stderr,"table park建表失败：%s\n",errmsg);
        sqlite3_free(errmsg);
        return-1;
    }
  //  printf("table_park建表成功！\n");
    return 0;
}
//曾英杰的表--------------------------